package com.sleepingcat.ft_flutter.initializer

import android.app.Application
import android.content.Context
import android.content.Intent
import androidx.startup.Initializer
import com.cat.android.lib_base.ext.logD
import com.idlefish.flutterboost.FlutterBoost
import com.idlefish.flutterboost.FlutterBoostDelegate
import com.idlefish.flutterboost.FlutterBoostRouteOptions
import com.sleepingcat.ft_flutter.activity.MyFlutterActivity
import com.sleepingcat.ft_flutter.activity.MyTransparentFlutterActivity
import com.sleepingcat.ft_flutter.activity.test.Test1Activity
import com.sleepingcat.ft_flutter.activity.test.Test2Activity
import com.sleepingcat.ft_flutter.channel.ChannelManager
import com.sleepingcat.lib_common.const.Routes

class FtFlutterInitializer : Initializer<Unit> {
    override fun create(context: Context) {
        initBoost(context)
    }

    override fun dependencies(): List<Class<out Initializer<*>>> {
        return emptyList()
    }

    private fun initBoost(context: Context) {
        FlutterBoost.instance().setup(context as Application, object : FlutterBoostDelegate {
            override fun pushNativeRoute(options: FlutterBoostRouteOptions) {
                "pushNativeRoute".logD()
                when (options.pageName()) {
                    Routes.Jetpack.JETPACK_MAIN -> {

                    }

                    "native/test1" -> {
                        val intent = Intent(context, Test1Activity::class.java)
                        FlutterBoost.instance().currentActivity().startActivity(intent)
                    }

                    "native/test2" -> {
                        val intent = Intent(context, Test2Activity::class.java)
                        FlutterBoost.instance().currentActivity().startActivity(intent)
                    }
                }
            }

            // 这个方法的触发时机是有两种：
            // 1.原生调用 FlutterBoost.instance().open()，open的是flutter路由，
            // 所以如果想在原生启动flutter页面，其实可以直接走这个方法里面的逻辑，如：FlutterImpl，
            // 另外原生调用pigeons打开flutter页面，其实也是走这里，所以当原生知道确定要跳哪个flutter页面时，其实可以不需用pigeon特意封装一个方法给原生调用，
            // 用pigeons封装给原生的主要优点是dart的路由不需要在原生那里再定义一次，因为pigeons里已经封装好

            // 2.dart调用 BoostNavigator.instance().push()，push的是flutter路由，并且withContainer = true
            override fun pushFlutterRoute(options: FlutterBoostRouteOptions) {
                "pushFlutterRoute".logD()
                // 其实应该把每个原生对应的路由经过一层hash算法，当成uniqueId传给flutter，
                // 这样如果出现这种：原生Act1 -> FlutterAct2 -> 原生Act3 -> FlutterAct4的容器，当FlutterAct想返回到Act1，就可以用那个uniqueId结合popRoute回调就可以实现
                // 每个原生Act都监听一个flow（liveData只有监听者活跃才有回调，不适合），popRoute就负责发这个数据，当然页面收到这个数据，判断是不是关闭这个页面
                val urlParams = HashMap<String, Any>()
                if (options.arguments() != null) {
                    urlParams.putAll(options.arguments())
                }
                val uniqueId = options.uniqueId()
                val url = options.pageName()
                // Android动态设置theme中，如果theme是透明了，则无效，所以只能分开两个Activity
                if (options.opaque()) {
                    MyFlutterActivity.startActivity(
                        FlutterBoost.instance().currentActivity(),
                        uniqueId,
                        url,
                        urlParams,
                    )
                } else {
                    MyTransparentFlutterActivity.startActivity(
                        FlutterBoost.instance().currentActivity(),
                        uniqueId,
                        url,
                        urlParams,
                    )
                }
            }

            override fun popRoute(options: FlutterBoostRouteOptions): Boolean {
                "popRoute - ${options.pageName()}".logD("CatApp")
                when (options.pageName()) {
                    "native/test1" -> {
                        FlutterBoost.instance()
                    }

                    "native/test2" -> {

                    }
                }
                return super.popRoute(options)
            }

        }) { enginee ->
            enginee.plugins.add(ChannelManager.plugins(enginee))
        }
    }
}